
package com.borqs.music.tasks;

import com.borqs.music.executor.Task;
import com.borqs.music.executor.TaskListener;
import com.borqs.music.http.HttpRequest;
import com.borqs.music.http.HttpResponse;
import com.borqs.music.http.RequestMethod;
import com.borqs.music.model.SingerInfo;
import com.borqs.music.util.MusicConf;
import com.borqs.music.util.MusicConf.ReqType;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.text.TextUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GetPersonalArtistAlbumTask extends Task {
    public static final int ARTIST = 1;
    public static final int ALBUM = 2;

    private String mTicket;
    private int type;

    public GetPersonalArtistAlbumTask(TaskListener taskListener, String ticket, int type) {
        super(taskListener);
        this.mTicket = ticket;
        this.type = type;
    }

    @Override
    public HttpRequest prepareRequest() {
        Map<String, String> params = new HashMap<String, String>();
        String url;
        if (type == ARTIST) {
            url = MusicConf.getUrl(ReqType.MUSIC_SINGER_OF_USER);
        } else if (type == ALBUM){
            url = MusicConf.getUrl(ReqType.MUSIC_ALBUM_OF_USER);
        } else {
            throw new IllegalStateException("Unknow type = " + type);
        }

        HttpRequest req = new HttpRequest(RequestMethod.GET, url, mTicket, params);
        return req;
    }

    @Override
    public Object handleResponse(HttpResponse resp) throws Exception {
        checkResponse(resp);
        JSONArray jArr = resp.asJSONArray();
        List<SingerInfo> singers = null;
        if (type == ARTIST)
            singers = decodeSingersJSONArray(jArr);
        else if (type == ALBUM)
            singers = decodeAblumesJSONArray(jArr);
        return singers;
    }

    private List<SingerInfo> decodeSingersJSONArray(JSONArray jArr) throws JSONException {
        List<SingerInfo> singers = new ArrayList<SingerInfo>(10);
        for (int i = 0, len = jArr.length(); i < len; i++) {
            JSONObject jObj;
            jObj = jArr.getJSONObject(i);
            SingerInfo singer = new SingerInfo();
            singer.name = jObj.getString("singer_name");
            singer.imgUrl = jObj.getString("photo_url");
            singer.count = jObj.getInt("count");
            
            if (!TextUtils.isEmpty(singer.name))
                singers.add(singer);
        }
        return singers;
    }


    private List<SingerInfo> decodeAblumesJSONArray(JSONArray jArr) throws JSONException {
        List<SingerInfo> albums = new ArrayList<SingerInfo>(10);
        for (int i = 0, len = jArr.length(); i < len; i++) {
            JSONObject jObj;
            jObj = jArr.getJSONObject(i);
            SingerInfo singer = new SingerInfo();
            singer.name = jObj.getString("album");
            singer.count = jObj.getInt("count");

            if (!TextUtils.isEmpty(singer.name))
                albums.add(singer);
        }
        return albums;
    }
}
